#include "Include.h"
vu16 PPGTA_copy,PPGTA_Min;

void PPG_Config()
{
	_ppgpc =0B10001101;		// PPG outputs high effective and low is ineffective 		
	_ppgc0 = 0B00001110;				
	_trgmod = 0;
	
	_ints = 1;		// Select C0VO as INT00 source		
	_cmp0dly = 0x3f & C_PPG_DL;		
	_ppgc1 = 0b00000100;
	//_ppgc2 = 0b00000000;
	_ppgc2 = 0b00000101;//+5
	
	_ppgtex= 0B01010101;  		
	_ppgta = 150;	
	_ppgtc = 150;   
	_ppgtd = 150;   		
							
	_ppgsaen = 0;  	
    
    _pwlt = PPGWIDTH_LIMIT_VALUE; // PPG Module pulse width limit
    
	// PPG non-repeatable trigger function 
	_tmr2c = 0b00000100;    //fPSC = fsys/16
	_tmr2 = 256 - PPG_UN_REPETABLE_VALUE; // 12us
	
    //------ ppg Approximation mode configuration------------------------
    _ppgatc0 = 0B11000000;
    _ppgatc1 = 0B10000010;
    _ppgatc2 = 0B00001010;
    _ppgtmc = 0B00000010;

    _ppgtmr1 = C_PPGTMR1;
    _ppgtmr2 = C_PPGTMR2;
    _ppgtmr3 = C_PPGTMR3;

    _ppghtmd = 1; //
    _ppgton = 0;  //

  
    //-----------------ppg2 init---------------------

    _ppgrt = 0x7F & 61; // ppg cycle (61+1)*8/fsys
    _ppgrten = 0;
    _ppgrn = 50; // ppg retrig times
    _ppg2c0 = 0x00;
    _ppg2c1 = 0;

						

}

void IGBT_PowerOn(void)
{
    PPG_Config();
    PPGTA_copy = C_InitPPGT;
    while (_pst0 == 1); // Wait PST bit low
    if (PPGTA_copy > 255)
    {
        _ppgta8 = 1;
    }
    else
    {
        _ppgta8 = 0;
    }
    _ppgta = PPGTA_copy & 0XFF; //

    bPowerState = 1; // PPG enable flag
    bStartPPG = 1;
}

void PowerOnProc()
{
    if (bErrPowerOff == 1)
        return;

//    bMachineOnState = ON; 	//20230616
    IH_Status_App.m_data = 0;	//20230625
    PanMoveInDet = C_PAN_DET_TIMER_100MS;
    NoPanDetectedCnt = 0;
    IGBT_Off();

    Prepare_PanDetect();
    PPGTA_copy = C_InitPPGT;
}

void PowerOffProc()
{
    IGBT_Off(); // IGBT_PowerOff();
    bMachineOnState = OFF;
     R_PowerIV = 0;
     PowerStep = 0;
     PPGTA_Min = C_MinPPGT;

}

void Adj_Power()
{
    vu16 PPGTC_Temp;
    vu16 PPGTD_Temp;

    if (bPowerAdjStart == 1)
    {
        bPowerAdjStart = 0;
        if (bLowPower == 0)
        {
            //  Change the initial value of PPG width to prevent power surge
            if (1 == HW_State_type1.bits.HW_Approach_Disable)
            {
                HW_State_type1.bits.HW_Approach_Disable = 0;
                PPGTA_copy = PPGTA_copy + 40;
            }

            if (PPGTA_copy > C_MaxPPGT)
            {
                PPGTA_copy = C_MaxPPGT; // ppg pulse width limite
            }
            else if (PPGTA_copy < PPGTA_Min/*C_MinPPGT*/)
            {
                //PPGTA_copy = C_MinPPGT;
                PPGTA_copy = PPGTA_Min;
            }

            while (_pst0 == 1); // Wait PST bit low
            if (PPGTA_copy > 255)
            {
                _ppgta8 = 1;
            }
            else
            {
                _ppgta8 = 0;
            }
            _ppgta = PPGTA_copy & 0XFF;

            // Hardware jitter====
            if (HW_State_type1.bits.HW_Approach)
            {
                //Approaching PPGTC
                PPGTC_Temp = PPGTA_copy + C_SHAKE_FRQ;
                if (PPGTC_Temp > C_MaxPPGT)
                {
                    PPGTC_Temp = C_MaxPPGT; // ppg pulse width limite
                }
	            else if (PPGTC_Temp < PPGTA_Min/*C_MinPPGT*/)
                {
	                //PPGTC_Temp = C_MinPPGT;
	                PPGTC_Temp = PPGTA_Min;
                }

                if (PPGTC_Temp > 255)
                {
                    _ppgtc8 = 1;
                }
                else
                {
                    _ppgtc8 = 0;
                }
                _ppgtc = PPGTC_Temp & 0xFF;

                ////Approaching PPGTD
                PPGTD_Temp = PPGTA_copy;

                if (PPGTD_Temp > C_MaxPPGT)
                {
                    PPGTD_Temp = C_MaxPPGT; // ppg pulse width limite
                }
	                else if (PPGTD_Temp < PPGTA_Min/*C_MinPPGT*/)
                {
	                    //PPGTD_Temp = C_MinPPGT;
	                    PPGTD_Temp = PPGTA_Min;
                }

                if (PPGTD_Temp > 255)
                {
                    _ppgtd8 = 1;
                }
                else
                {
                    _ppgtd8 = 0;
                }
                _ppgtd = PPGTD_Temp & 0xFF;


				_ppgatc1 = 0B10000010;
				_ppgatc2 = 0B00001010;

                _ppghtmd = 0; // Control Approach mode 1:  ppgton 0: C4VOD RISING
                
                _ppgsamd = 1; // HW mode
                
                _ppgsaen = 1;
              
            }
            else
            {
            	_ppgsamd = 0;
            	_ppgsaen = 0;
            	
                _ppghtmd = 1; // Approach mode enable
                _ppgton = OFF;
            }
        }

        else if (1 == bLowPowerADJ) // LowPowerADJ
        {
            if (PPGTA_copy > C_MaxPPGT_l)
            {
                PPGTA_copy = C_MaxPPGT_l; // ppg pulse width limite
            }
            else if (PPGTA_copy < C_MinPPGT_l)
            {
                PPGTA_copy = C_MinPPGT_l;
            }

            while (_pst0 == 1); // Wait PST bit low
            if (PPGTA_copy > 255)
            {
                _ppgta8 = 1;
            }
            else
            {
                _ppgta8 = 0;
            }
            _ppgta = PPGTA_copy & 0XFF;
        }
    }
}
